package com.aua.book.android.xml;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import android.app.Activity;
import android.os.Bundle;
import android.text.Html;
import android.util.TypedValue;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;

public class AndroidBookActivity extends Activity {

  // for showing text of the book
  private TextView textView;
  // for showing percentage of zoom
  private TextView zoomTextView;
  // for showing number of current page
  private TextView progressTextView;
  // for number of all pages
  private TextView numOfPagesText;
  // for showing progress by dots
 
  private static int lastPage = 0;
  private static int currentPage = 0;
  private static float currentSize = 20;
  private static int progress = 0;

  private String bookText = "";
  private ScrollView scroller;

  static final int NONE = 0;
  static final int ZOOM = 1;
  int mode = NONE;
  private List<String> pages = new ArrayList<String>();

  static int ZOOM_COEF = 1;

  int oldDist = 1;

  int markedPage;
  boolean isBookMarked = false;
  
  // private View.OnTouchListener gestureListener;
  private GestureListener gestureListener = new GestureListener(this);

  private final int zoomInId = 1;
  private final int zoomOutId = 2;
  private final int initialSizeId = 3;
  private final int leftId = 4;
  private final int rightId = 5;
  private final int setBookmarkId = 6;
  private final int goToId = 7;
  
  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
    menu.add(NONE, zoomInId, zoomInId, R.string.zoomIn);
    menu.add(NONE, zoomOutId, zoomOutId, R.string.zoomOut);
    menu.add(NONE, initialSizeId, initialSizeId, R.string.initialSize);
    menu.add(NONE, leftId, leftId, R.string.left);
    menu.add(NONE, rightId, rightId, R.string.right);
    menu.add(NONE, setBookmarkId, setBookmarkId, R.string.setBookmark);
    menu.add(NONE, goToId, goToId, R.string.GoTo);
    return super.onCreateOptionsMenu(menu);
  }

  @Override
  public boolean onOptionsItemSelected(MenuItem item) {
    // Handle item textView
    switch (item.getItemId()) {
    case zoomInId:
      currentSize = ZoomHelper.zoomIn(this, Float.parseFloat(getString(R.string.buttonZoom)), textView,
          zoomTextView);
      break;
    case zoomOutId:
      currentSize = ZoomHelper.zoomOut(this, Float.parseFloat(getString(R.string.buttonZoom)), textView,
          zoomTextView);
      break;
    case initialSizeId:
      currentSize = ZoomHelper.initialSize(this, textView, zoomTextView);
      break;
    case leftId:
      currentPage = PageTurnHelper.turnLeft(this,progressTextView, textView, pages, currentPage, lastPage);
      break;
    case rightId:
      currentPage = PageTurnHelper.turnRight(this,progressTextView, textView, pages, currentPage, lastPage);
      break;
    case setBookmarkId:
      markedPage = currentPage;
      isBookMarked = true;
      Toast.makeText(this, "set", 900000).show();
      break;
    case goToId:
     currentPage= PageTurnHelper.goToPage(this, progressTextView, textView, pages, markedPage, lastPage);
     Toast.makeText(this, "gnac", 900000).show();
      break;
    }

    return true;
  }

  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
   ProgressDisplay.displayProgress(this,progress);
    

    /*
     * image.setPadding(30, 0, 0, 0); linearLayoutDots.setOnTouchListener(new View.OnTouchListener() { public
     * boolean onTouch(View v, MotionEvent event) { image.setPadding((int) event.getX(), 0, 0, 0); return
     * false; } });
     */
    textView = (TextView) findViewById(R.id.item_title);
    textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, currentSize);
    scroller = (ScrollView) findViewById(R.id.scroller);
    zoomTextView = (TextView) findViewById(R.id.zoomText);
    numOfPagesText = (TextView) findViewById(R.id.numOfPagesText);
    progressTextView = (TextView) findViewById(R.id.currentPageText);

    scroller.setOnTouchListener(gestureListener);
    try {
      InputStream is = getAssets().open("source.xml");

      DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
      Document doc = builder.parse(is, null);
      NodeList text = doc.getElementsByTagName("title");
      bookText += ((Element) text.item(0)).getTextContent();
      text = doc.getElementsByTagName("body");
      bookText += ((Element) text.item(0)).getTextContent();
      PageCreator.creat(this, (float) currentSize);
      show();
      is.close();

    } catch (Throwable t) {

      Toast.makeText(this, "Exception :" + t.toString(), 900000000).show();

    }

  }

  public void show() {
    textView.setText(Html.fromHtml(pages.get(currentPage)));
    numOfPagesText.setText("" + lastPage + "pgs");
    int progress = currentPage * 100 / lastPage;
    progressTextView.setText("Progress " + progress + "%");
    int resid = ZoomHelper.getZoomP(this, textView);
    zoomTextView.setText("" + resid + "%");

  }

 
  public TextView getTextView() {
    return textView;
  }

  public void setTextView(TextView textView) {
    this.textView = textView;
  }

  public TextView getZoomTextView() {
    return zoomTextView;
  }

  public void setZoomTextView(TextView zoomText) {
    this.zoomTextView = zoomText;
  }

   public void setProgressTextView(TextView progT) {
    this.progressTextView = progT;
  }
   
   
   public TextView getProgressTextView( ) {
     return progressTextView;
   }

  public int getCurrnetPage() {
    return currentPage;
  }

  public void setCurrnetPage(int pages) {
    currentPage = pages;
  }

  public int getLastPage() {
    return lastPage;
  }

  public void setLastPage(int pages) {
    lastPage = pages;
  }

  public int getProgress() {
    return progress;
  }

  public void set(int pages) {
    lastPage = pages;
  }
 

  public List<String> getPages() {
    return pages;
  }

  public void setPages(List<String> pages) {
    this.pages = pages;
  }

  public float getCurrentSize() {
    return currentSize;
  }

  public void setCurrentSize(float size) {
    currentSize = size;
  }

  public TextView getNumberOfPages() {
    return numOfPagesText;
  }

  public void setNumOfPagesText(TextView num) {
    this.numOfPagesText = num;
  }

  public String getBookText() {
    return bookText;
  }

  public void setBookText(String str) {
    this.bookText = str;
  }

}